using Identity.API.Data;
using Identity.API.Models;
using Identity.API.Service;
using IdentityServer4.Services;
using IdentityServer4.Configuration;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
using System;
using System.Reflection;
using Identity.API.StartupConfigurationServices;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Authentication.Cookies;

namespace Identity.API
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var ConnectionString = Configuration.GetConnectionString("MySqlDev");
            var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;

            #region dbcontext
            //services.AddDbContext<CustomDbContext>(options =>
            //    options.UseMySQL(ConnectionString, mysqlOptions=>
            //    {
            //        mysqlOptions.MigrationsAssembly(migrationsAssembly);
            //    }));
            services.AddDbContext<ApplicationDbContext>(options =>
            {
                options.UseMySql(ConnectionString, new MySqlServerVersion(new Version(10, 1, 40)), mysqlOptions =>
                {
                    mysqlOptions.MigrationsAssembly(migrationsAssembly);
                    mysqlOptions.EnableRetryOnFailure(maxRetryCount: 15, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null);
                });
            });
            #endregion

            #region identity
            //services
            //    .AddIdentity<User, Role>()
            //    .AddEntityFrameworkStores<CustomDbContext>()
            //    .AddDefaultTokenProviders();
            services
                .AddIdentity<ApplicationUser, IdentityRole>(options =>
                {
                    options.Password.RequireDigit = false;
                    options.Password.RequiredLength = 4;
                    options.Password.RequireNonAlphanumeric = false;
                    options.Password.RequireUppercase = false;
                    options.Password.RequireLowercase = false;
                })
                .AddEntityFrameworkStores<ApplicationDbContext>()
                .AddDefaultTokenProviders();
            #endregion


            #region identityserver aspnetcore.identity
            services.AddIdentityService(Configuration, migrationsAssembly);
            #endregion
            services.AddControllersWithViews();
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo { Title = "Identity.API", Version = "v1" });
            });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseSwagger();
                app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Identity.API v1"));
            }

            app.UseStaticFiles();

            //app.UseHsts();
            app.UseIdentityServer();
            app.UseCookiePolicy(new CookiePolicyOptions { MinimumSameSitePolicy = SameSiteMode.Lax });
            app.UseRouting();
            app.UseAuthorization();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}
